/*
This file is part of CanFestival, a library implementing CanOpen Stack.

Copyright (C): Edouard TISSERANT and Francis DUPIN

See COPYING file for copyrights details.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#ifndef __can_driver_h__
#define __can_driver_h__

#define CW  1 // clock wise
#define CCW 2 // counter clock wise
#define NODE_NUM	3
#define ENABLE		1
#define DISABLE		2


struct struct_s_BOARD;

typedef struct struct_s_BOARD s_BOARD;
typedef int* CAN_HANDLE;
typedef unsigned char UNS8;
typedef unsigned short UNS16;
typedef unsigned int UNS32;


struct struct_s_BOARD {
  char * busname;  /**< The bus name on which the CAN board is connected */
  char * baudrate; /**< The board baudrate */
};

/**
 * @brief The CAN message structure
 * @ingroup cans
 */
typedef struct {
  UNS32 cob_id; /**< message's ID */
  UNS8 rtr;             /**< remote transmission request. (0 if not rtr message, 1 if rtr message) */
  UNS8 len;             /**< message's length (0 to 8) */
  UNS8 data[8]; /**< message's datas */
} Message;


typedef struct{
	UNS8 node_id;
	//UNS8 state;
	enum 
	{
		NODE_UNDEF,
		NODE_VALID,
		NODE_INVALID
	}state;

	UNS8 PDOFLAG;
	UNS16 stateReg;
}NodeInfor;


typedef struct
{
	UNS16 velocity;//unit rps/s
	UNS16 acceleration;//rps/s
	UNS16 deceleration;//rps/s
	UNS32 targetposition;//steps
	UNS8  dirflag;// flag to indicate motor runs CW or CCW

	/* home parameters */
	UNS16 home_velocity;
	UNS16 home_acceleration;
	UNS16 home_index_velocity;
	UNS32 home_offset;
	UNS8  home_switch;
	UNS8  home_method;
	
}PositionParam_t;



int canInit_driver(void);
CAN_HANDLE canOpen_driver (void);
UNS8  canReceive_driver (CAN_HANDLE fd0, Message * m);
UNS8 canSend_driver (CAN_HANDLE fd0, Message const * m);
int canMotor_Test(CAN_HANDLE fd0,int id);
int canMotor_GetStatus(CAN_HANDLE fd0,int id,unsigned int *status);
int canMotor_Parse(Message *pmsg);
int canMotor_Test_CCW(CAN_HANDLE fd0,int id);
int canMotor_Home(CAN_HANDLE fd0,int id,PositionParam_t*param);
int canMotor_HomeModeTest(CAN_HANDLE fd0,int id);
int canMotor_FilterConfig(CAN_HANDLE fd0,int id);
int canMotor_Enable(CAN_HANDLE fd0,int id);










#endif


